import os
from datetime import timedelta
from dotenv import load_dotenv

load_dotenv()

class Config:
    """Docker环境配置类"""
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'blockchain-middleware-secret-key-2024'
    
    # MongoDB配置
    MONGODB_URI = os.environ.get('MONGODB_URI') or 'mongodb://root:Simmed.2018@192.168.1.35:19060/blockchain-middleware?authSource=admin'
    DATABASE_NAME = 'blockchain-middleware'
    
    # JWT配置
    JWT_SECRET_KEY = os.environ.get('JWT_SECRET_KEY') or SECRET_KEY
    JWT_ACCESS_TOKEN_EXPIRES = timedelta(hours=24)
    
    # API配置
    API_VERSION = 'v1'
    MAX_CONTENT_LENGTH = 16 * 1024 * 1024  # 16MB
    
    # Redis配置
    REDIS_HOST = os.environ.get('REDIS_HOST') or '192.168.1.27'
    REDIS_PORT = int(os.environ.get('REDIS_PORT', '6379'))
    REDIS_PASSWORD = os.environ.get('REDIS_PASSWORD') or 'Simmed.r022'
    REDIS_DB = int(os.environ.get('REDIS_DB', '0'))
    REDIS_DECODE_RESPONSES = True
    
    # 缓存配置
    CACHE_DEFAULT_TIMEOUT = int(os.environ.get('CACHE_DEFAULT_TIMEOUT', '300'))  # 5分钟
    CACHE_KEY_PREFIX = os.environ.get('CACHE_KEY_PREFIX') or 'blockchain_cache'
    
    # 区块链适配器配置
    SUPPORTED_CHAINS = {
        'fisco_bcos': {
            'name': 'FISCO BCOS',
            'version': '2.9.1',
            'front_host': os.environ.get('FISCO_FRONT_HOST', '47.103.91.229'),
            'front_port': os.environ.get('FISCO_FRONT_PORT', '5002'),
            'rpc_url': os.environ.get('FISCO_BCOS_RPC_URL') or 'http://47.103.91.229:5002',
            'group_id': int(os.environ.get('FISCO_BCOS_GROUP_ID', '1')),
            'chain_id': int(os.environ.get('FISCO_BCOS_CHAIN_ID', '1')),
            # 默认合约和用户配置
            'default_contract_address': os.environ.get('FISCO_BCOS_CONTRACT_ADDRESS', '0x45a06cc767543fd15547ea75e0809a6e70234406'),
            'default_user_address': os.environ.get('FISCO_BCOS_USER_ADDRESS', '0x49795c5e26929760875a9cc1416f355647d763ed'),
            'default_contract_id': os.environ.get('FISCO_BCOS_CONTRACT_ID', '1'),
        },
        'chainmaker': {
            'name': 'ChainMaker',
            'version': '2.4.0',
            'chain_id': os.environ.get('CHAINMAKER_CHAIN_ID') or 'simmed',
            'org_id': os.environ.get('CHAINMAKER_ORG_ID') or 'TestCMorg1',
            'config_path': os.environ.get('CHAINMAKER_CONFIG_PATH') or '/app/adapters/chainmaker/sdk_config_docker.yml'
        },
        'jdchain': {
            'name': 'JD Chain',
            'version': '1.6.0',
            'chain_id': 'jdchain',
            'gateway_host': os.environ.get('JDCHAIN_GATEWAY_HOST') or '192.168.1.57',
            'gateway_port': int(os.environ.get('JDCHAIN_GATEWAY_PORT', '8080')),
            'ledger_hash': os.environ.get('JDCHAIN_LEDGER') or 'j5sxh3PeRF2kQdZziQmbaxUyd4Y83Fmkdj97qyb4gyYGBc',
            'java_home': os.environ.get('JAVA_HOME', ''),
            'jdchain_sdk_path': os.environ.get('JDCHAIN_SDK_PATH', ''),
            'bridge_jar_path': os.environ.get('BRIDGE_JAR_PATH', ''),
            # 用户凭证
            'user_name': os.environ.get('JDCHAIN_USER_NAME', 'admin'),
            'user_pub_key': os.environ.get('JDCHAIN_USER_PUB_KEY', '7VeREJX3BVQb4fRxve6zPTxWPQfy19qiafaxrBCdBYeqkMLm'),
            'user_priv_key': os.environ.get('JDCHAIN_USER_PRIV_KEY', '177gk1RkeU3jJsuTxCqg9AmNn3Fi8aidij3jShyFAtGSwC313euKGnaKXpzpumbuiV6NdWD'),
            'admin_password': os.environ.get('JDCHAIN_ADMIN_PASSWORD', 'AXhhKihAa2LaRwY5mftnngSPKDF4N9JignnQ4skynY8y'),
            'contract_address': os.environ.get('JDCHAIN_CONTRACT_ADDRESS', 'LdeP2tbSkUdDgGzGNULKxVwByzY7T9WUkPa9z'),
            'data_account_address': os.environ.get('JDCHAIN_DATA_ACCOUNT_ADDRESS', 'LdeNwQWabrf6WSjZ35saFo52MfQFhVKvm11aC')
        },
        'xuperchain': {
            'name': 'XuperChain',
            'version': '4.0.0',
            'rpc_url': os.environ.get('XUPERCHAIN_RPC_URL') or 'http://192.168.1.57:37301',
            'chain_name': os.environ.get('XUPERCHAIN_CHAIN_NAME') or 'xuper',
            'default_contract': os.environ.get('XUPERCHAIN_DEFAULT_CONTRACT') or 'simple_storage',
            # 账号配置
            'keys_dir': os.environ.get('XUPERCHAIN_KEYS_DIR') or '/app/contracts/xuper/keys',
            'default_account': os.environ.get('XUPERCHAIN_DEFAULT_ACCOUNT') or '',
            # 合约配置
            'wasm_contract_path': os.environ.get('XUPERCHAIN_WASM_CONTRACT_PATH') or '/app/contracts/xuper/simplestorage-cpp/simplestorage.wasm',
            'contract_init_args': {},
            # SDK配置
            'timeout': int(os.environ.get('XUPERCHAIN_TIMEOUT', '30')),
            'use_sdk': os.environ.get('XUPERCHAIN_USE_SDK', 'True').lower() == 'true'
        },
        'thanos': {
            'name': 'Thanos Chain',
            'version': '1.0.0',
            'chain_id': os.environ.get('THANOS_CHAIN_ID') or 'thanos',
            'api_url': os.environ.get('THANOS_API_URL') or 'http://192.168.1.57:8581',
            'timeout': int(os.environ.get('THANOS_TIMEOUT', '30'))
        }
    }
    
    # 日志配置
    LOG_LEVEL = os.environ.get('LOG_LEVEL', 'INFO')
    LOG_FORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
    
    # 开发模式
    DEBUG = os.environ.get('DEBUG', 'False').lower() == 'true'
    TESTING = False

class DevelopmentConfig(Config):
    """开发环境配置"""
    DEBUG = True

class ProductionConfig(Config):
    """生产环境配置"""
    DEBUG = False

class TestingConfig(Config):
    """测试环境配置"""
    TESTING = True
    DEBUG = True

# 配置字典
config = {
    'development': DevelopmentConfig,
    'production': ProductionConfig,
    'testing': TestingConfig,
    'default': DevelopmentConfig
}